package frmwrk.db.internal.classes;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import utils.Tracer;
import frmwrk.db.publicInterface.IConnectionManager;
import frmwrk.db.publicInterface.ISession;

public class CConnectionManager implements IConnectionManager, Runnable{
	
	private static String connString = "jdbc:h2:~/Dropbox/Java/Deutsch/DB/db;schema=user";
	private static String username = "deutsch";
	private static String password = "orion";
	private static Connection conn;
	
//	public ISession getSession(){
//    ISession newSession = null;
//		try {
//    	newSession = new CSession(getConnection().createStatement());
//    } catch (SQLException e) {
//  		Tracer.trace("ConnectionManager.Error creating session!", e);
//    }
//		return newSession;
//	}
	
	public void connect() {
		try {
			Tracer.trace("ConnectionManager.Connecting");
			Class.forName("org.h2.Driver");
			conn = DriverManager.getConnection(connString, username, password);	

			if (conn == null)
				Tracer.trace("ConnectionManager.Connection failed!");
			else
				Tracer.trace("ConnectionManager.Connected!");
		} catch (Exception e) {
			Tracer.trace(e);
		}  
	}

	public void disconnect() {
		try {
			conn.close();
			Tracer.trace("ConnectionManager.Disconnected!");			
		} catch (SQLException e) {
			Tracer.trace("ConnectionManager.Disconnected failed!");			
			Tracer.trace(e);
		}			
	}

	@Override
  public void run() {
		connect();
  }

	@Override
  public ISession getSession() {
	  return null;
  }
  
	

}
